home *** CD-ROM | disk | FTP | other *** search
/ Aminet 3 / Aminet 3 - July 1994.iso / Aminet / misc / unix / tracker_4_3.lzh / tracker / getopt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-13  |  2.1 KB  |  117 lines

  1. /* getopt.c 
  2.     vi:se ts=3 sw=3:
  3.  */
  4.  
  5. /* $Id: getopt.c,v 4.0 1994/01/11 17:48:21 espie Exp espie $
  6.  * $Log: getopt.c,v $
  7.  * Revision 4.0  1994/01/11  17:48:21  espie
  8.  * Small changes.
  9.  *
  10.  * Revision 1.2  1994/01/09  17:36:22  Espie
  11.  * Generalized open.c.
  12.  *
  13.  * Revision 1.1  1993/12/26  00:55:53  Espie
  14.  * Initial revision
  15.  *
  16.  * Revision 1.5  1993/12/04  16:12:50  espie
  17.  * New getopt semantics.
  18.  *
  19.  * Revision 1.4  1993/11/17  15:31:16  espie
  20.  * *** empty log message ***
  21.  *
  22.  * Revision 1.3  1993/01/26  14:10:38  espie
  23.  * Fixed up stupdi end of file bug.
  24.  *
  25.  * Revision 1.2  1992/11/27  10:29:00  espie
  26.  * General cleanup
  27.  *
  28.  */
  29.  
  30. #include <stdio.h>
  31. #include <ctype.h>
  32. #include <string.h>
  33.  
  34. #include "defs.h"
  35. #include "getopt.h"
  36.  
  37. ID("$Id: getopt.c,v 4.0 1994/01/11 17:48:21 espie Exp espie $")
  38. int optind = 1;
  39. char *optarg = 0;
  40. LOCAL not_an_option = 0;
  41.  
  42. LOCAL int parse_option(argv, option)
  43. char *argv[];
  44. struct long_option *option;
  45.     {
  46.     optind++;
  47.     if (option->argn)
  48.         optarg = argv[optind++];
  49.     return option->code;
  50.     }
  51.  
  52. int getlongopt(argc, argv, options)
  53. int argc;
  54. char *argv[];
  55. struct long_option *options;
  56.     {
  57.     if (not_an_option == optind)
  58.         return -1;
  59.     if (optind >= argc)
  60.         return -1;
  61.     if (argv[optind][0] == '-')
  62.         {
  63.         char *match = argv[optind]+1;
  64.         if (strlen(match) == 1)
  65.             {
  66.             if (match[0] == '-')
  67.                 {
  68.                 not_an_option = ++optind;
  69.                 return -1;
  70.                 }
  71.             while(options->fulltext)
  72.                 {
  73.                 if (options->abbrev == match[0])
  74.                     return parse_option(argv, options);
  75.                 else
  76.                     options++;
  77.                 }
  78.             return -1;
  79.             }
  80.         else
  81.             {
  82.             int max_match = 0;
  83.             struct long_option *best = 0;
  84.  
  85.             while (options->fulltext)
  86.                 {
  87.                 int i;
  88.                 for (i = 0; ; i++)
  89.                     {
  90.                     if (options->fulltext[i] == 0 && match[i] == 0)
  91.                         return parse_option(argv, options);
  92.                     if (match[i] == 0)
  93.                         {
  94.                         if (i > max_match)
  95.                             {
  96.                             max_match = i;
  97.                             best = options;
  98.                             }
  99.                         break;
  100.                         }
  101.                     if (tolower(options->fulltext[i]) != tolower(match[i]))
  102.                         break;
  103.                     }
  104.                 options++;
  105.                 }
  106.             if (max_match < 3)
  107.                 {
  108.                 fprintf(stderr, "Unrecognized option: %s\n", match);
  109.                 return -1;
  110.                 }
  111.             return parse_option(argv, best);
  112.             }
  113.         }
  114.     else
  115.         return -1;
  116.     }
  117.